package game;

import java.awt.*;
import java.awt.event.KeyEvent;

/**
 * 描述：游戏物体的根类
 *
 * @author wangb
 * @version 1.0
 * create 2018-11-01-23:01
 */
public class GameObject{
     boolean left ,right,up,down;
     Image img;
     double x , y ;
     int speed = 2;//初始速度
     int width,height;

    /**
     * 构造器
     * @param img 图片
     * @param x  x
     * @param y y
     */
    public GameObject(Image img , double x , double y) {
        this.img = img;
        this.x = x;
        this.y = y;
    }

    /**
     * 无参构造器
     */
    public GameObject( ) {
    }


    public void drawSelf(Graphics g){
         g.drawImage (img,(int)x,(int)y,null);//画出物体

     }

    public GameObject(Image img,double x ,double y ,int speed,int width ,int height){
          this.img = img;
          this.x = x ;
          this.y = y ;
          this.speed = speed;
          this.width = width;
          this.height = height;


 }

    /**
     * 把物体所在的矩形返回
     * @return 物体所在的矩形,便于后续的碰撞检测。
     */
    public Rectangle getRect(){
        return new Rectangle ((int)x,(int)y,width,height);
    }

    /**
     * 按下某个键，增加相应的方向
     * @param e
     */
    public void addDirection(KeyEvent e){
        switch (e.getKeyCode ()){

            case KeyEvent.VK_LEFT:
                left = true;
                break;
            case KeyEvent.VK_UP:
                up = true;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                break;
            case KeyEvent.VK_DOWN:
                down = true;
                break;

        }
    }

    /**
     * 减某个方向。
     * @param e
     */
    public void minusDirection(KeyEvent e){
        switch (e.getKeyCode ()){

            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_UP:
                up = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;

        }
    }
}
